package testcase.app;

import java.io.IOException;
import java.text.ParseException;

import javax.xml.parsers.ParserConfigurationException;

import org.json.JSONException;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.xml.sax.SAXException;


import api.app.CloseSubject;
import api.app.StartChat;
import api.app.Subject;
import api.doctor.UpdateStatus;
import common.Config;
import common.Host;
import common.TestData;

/**
 * 帮忙医APP获取用户咨询测试用例
 * @author gaowen
 *
 */
public class TestSubject {
	/**
	 * 帮忙医APP获取用户咨询接口测试环境数据驱动
	 * @return 测试数据
	 * @throws IOException
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 */
	@DataProvider (name="Test")
	public static String[][] testSubjectDataProvider() throws IOException, ParserConfigurationException, SAXException
	{
		Host.setup("Test");
		TestData tData=new TestData("app/TestSubject.xml");
		return tData.getParam();
	}
	@BeforeClass(groups="Test")
	public void testSubjectBeforeTest() throws IOException, JSONException, ParseException, ParserConfigurationException, SAXException
	{
		//设置环境
		Host.setup("Test");
		//准备数据
		String doctorID=Config.getValue("Doctor-Test", "ID");
		String token=Config.getValue("Doctor-Test", "token");
		//上线医生
		UpdateStatus.put("true", "1", doctorID, token);
		//开启会话并获取SubjectID
		String resutl=StartChat.post(11200,"机器人R1", "Man",20,"121509150082", "PKZIbEe+K+DDEVP0VXBLzg==");
		JSONObject jsonResult=new JSONObject(resutl);
		String subjectID=jsonResult.getJSONObject("Data").getString("SubjectID");
		//关闭会话
		CloseSubject.get(subjectID, "PKZIbEe+K+DDEVP0VXBLzg==");
		//下线医生
		UpdateStatus.put("false", "1", doctorID, token);
	}
	/**
	 * 帮忙医APP获取用户咨询接口测试环境测试用例
	 * @param caseName - 用例名称
	 * @param UserAccount - 用户账号
	 * @param PassWord - 用户密码
	 * @throws JSONException
	 * @throws IOException
	 * @throws ParseException
	 */
	@Test (dataProvider="Test",groups="Test")
	public void testSubjectTestCase(String caseName,String token) throws JSONException, IOException, ParseException
	{
		//获取用户token
		//调用ReportList接口并将接口返回字符串转为JSON格式
		String result=Subject.get(token);
		JSONObject jsonObject;
		if (result==null || result.isEmpty())
		{
			jsonObject = null;
		}
		else
		{
			jsonObject = new JSONObject(result);
		}
		String checkpoint=jsonObject.getJSONObject("Data").getString("HasValue");
		switch (caseName) {
		case "HasSubject":
			Assert.assertEquals(checkpoint, "true");
			break;
		case "NoSubject":
			Assert.assertEquals(checkpoint, "false");
			break;
		default:
			break;
		}
		
	}
}
